package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
//        报运单、报运单下的货物（从购销合同下的货物表中获取）、报运单下的附件（从购销合同下的附件表中获取）
//        1、查询这批购销合同    select * from co_contract where id in (111,323)
        String[] contractIds = export.getContractIds().split(",");
//        把数组转成集合，为了in查询使用
        List<String> contractIdList = Arrays.asList(contractIds);
        ContractExample contractExample  = new ContractExample();
        contractExample.createCriteria().andIdIn(contractIdList);
        List<Contract> exportList = contractDao.selectByExample(contractExample);
        int totalProNum = 0;
        int totalExtNum = 0;
        StringBuffer sb = new StringBuffer("");
        for (Contract contract : exportList) {
//        报运单还需要有的数据是：1、这批购销合同下的货物数量、附件数量
//                                2、这批购销合同的总金额
//                                3、这批购销合同的合同号
            totalProNum+=contract.getProNum();
            totalExtNum+=contract.getExtNum();
            sb.append(contract.getContractNo()).append(" ");
        }



//        开始处理货物  selecct * from 货物表 where contract_id in ()
        ContractProductExample cpExample = new ContractProductExample();
        cpExample.createCriteria().andContractIdIn(contractIdList);
//        购销合同下的货物列表
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(cpExample);
        for (ContractProduct contractProduct : contractProductList) {
//            有多少个购销合同下的货物就创建多少个报运单下的货物对象
            ExportProduct exportProduct = new ExportProduct();
            org.springframework.beans.BeanUtils.copyProperties(contractProduct,exportProduct);
            exportProduct.setExportId(export.getId());
            exportProductDao.insertSelective(exportProduct);

//            货物下的附件
            List<ExtCproduct> extCproductList = contractProduct.getExtCproducts();
            for (ExtCproduct extCproduct : extCproductList) {
//                有多少个购销合同下的附件就创建多少个报运单下的附件对象
                ExtEproduct extEproduct = new ExtEproduct();
//                报运单附件和购销合同附件属性比较：需要手动设置报运单附件的两个属性：exportProductId和exportId
                org.springframework.beans.BeanUtils.copyProperties(extCproduct,extEproduct);
                extEproduct.setExportId(export.getId());
                extEproduct.setExportProductId(exportProduct.getId());
                extEproductDao.insertSelective(extEproduct);
            }
        }
        export.setProNum(totalProNum);
        export.setExtNum(totalExtNum);
        export.setCustomerContract(sb.toString());//用空格隔开
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        List<ExportProduct> exportProducts = export.getExportProducts();

        if(exportProducts!=null&&exportProducts.size()>0){
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
        exportDao.updateByPrimaryKeySelective(export);
    }

    @Override
    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
//        private String exportId;
//        private Integer state;
//        private String remark;
//        private Set<ExportProductResult> products;
//        1、报运单的状态
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);
//     2、更新货物的税
        Set<ExportProductResult> products = exportResult.getProducts();
        for (ExportProductResult product : products) {
//            update 货物表 set tax=? where id=?
            ExportProduct exportProduct = new ExportProduct();
            exportProduct.setId(product.getExportProductId());
            exportProduct.setTax(product.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }

    @Override
    public List<Export> findByState(int state) {
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andStateEqualTo((long)state);
        return exportDao.selectByExample(exportExample);
    }
}
